<?php
class Resource_Layout {
	public function __construct() {
		$layout_config = ZendX_Config_Manager::getInstance()->getConfig('layout');
		$view = Zend_Layout::startMvc($layout_config)->getView();
		
		if(isset($layout_config->doctype)) {
			$view->doctype($layout_config->doctype);
		}
		
		if(isset($layout_config->scripts) && !empty($layout_config->scripts->items)) {
			foreach($layout_config->scripts->items as $item) {
				$func = 'appendFile';
				if(isset($item->prepend)) {
					$func = $item->prepend ? 'prependFile' : 'appendFile';
				}
				$path = "{$layout_config->scripts->baseDir}/{$item->path}";
				if(isset($item->extern)) {
					$path = $item->extern ? $item->path : "{$layout_config->scripts->baseDir}/{$item->path}";
				}
				$view->headScript()->$func($path);
			}
		}
		
		if(isset($layout_config->styles) && !empty($layout_config->styles->items)) {
			foreach($layout_config->styles->items as $item) {
				$func = 'appendStylesheet';
				if(isset($item->prepend)) {
					$func = $item->prepend ? 'prependStylesheet' : 'appendStylesheet';
				}
				$path = "{$layout_config->styles->baseDir}/{$item->path}";
				if(isset($item->extern)) {
					$path = $item->extern ? $item->path : "{$layout_config->styles->baseDir}/{$item->path}";
				}
				$view->headLink()->$func($path);
			}
		}
	}
}